package com.zly.lab.blog.utils;

import com.zly.lab.blog.entity.Article;
import org.commonmark.node.Node;
import org.commonmark.parser.Parser;
import org.commonmark.renderer.html.HtmlRenderer;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Commons {
    public static String dateFormat(Date date){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = simpleDateFormat.format(date);
        return format;
    }
    public static  String  permalink(Integer articleId){
        return "/blog/articleDetial/"+articleId;
    }

    //截取文章摘要
    public static String intro(Article article, Integer length){
        //获取文章内容
        String content = article.getContent();
        if(content.length()>length){
            //如果文章够长,截取
            //截取指定长度
            String substring = content.substring(0, length);
            return substring+"......";
        }
        //如果文章不够长,直接显示
        return content;
    }

    public static String show_thumb(Article article){
        String thumbnail = article.getThumbnail();
        if (thumbnail!=null && thumbnail!=""){
            return thumbnail;
        }else{
            return "/user/img/rand/"+article.getId()+".png";
        }
    }
    //文章内容
    public static String article(String content){
        // 创建解析器和渲染器实例
        Parser parser = Parser.builder().build();
        HtmlRenderer renderer = HtmlRenderer.builder().build();
        // 解析 CommonMark 文本为节点树
        Node document = parser.parse(content);
        // 渲染节点树为 HTML 字符串
        return renderer.render(document);
    }

    //回到前台首页
    public static String site_url(){
        return "/";
    }

    public static String site_url(String url){
        return url;
    }
}
